package com.itheima.tlias.web.controller;

import com.itheima.tlias.anno.LogAnno;
import com.itheima.tlias.domain.Emp;
import com.itheima.tlias.service.EmpService;
import com.itheima.tlias.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * @author ghy
 * @version 1.0.1
 * @date 2024-01-28 10:41:01
 */
@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 根据ID批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    @LogAnno(methodDesc = "根据ID批量删除")
    public Result deleteByIds(@PathVariable(name = "ids") List<Integer> ids) {
        log.info("EmpController delete ids:{}", ids);
        return empService.deleteByIds(ids);
    }

    @PutMapping
    @LogAnno(methodDesc = "更新员工")
    public Result update(@RequestBody Emp emp) {
        log.info("EmpController update emp:{}", emp);
        return empService.update(emp);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable(name = "id") Integer id){
        log.info("EmpController findById id:{}", id);
        return empService.findById(id);
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    @LogAnno(methodDesc = "分页条件查询")
    public Result page(
            String name, Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        log.info("EmpController page -->name:{}, gender:{}, begin:{}, end:{},page:{}, pageSize:{}", name, gender, begin, end,page, pageSize);
        return empService.page(name, gender, begin, end, page, pageSize);
    }

}
